from fastapi import APIRouter
from sqlalchemy import text
from app.database.database import SessionLocal, logger
from app.core.config import settings

router = APIRouter()
# 使用属性访问方式获取配置
app_config = settings.app_config

# 根路径
@router.get("/")
async def root():
    return {"message": f"{app_config.name} is running", "version": app_config.version}

# 健康检查 - 增强版，包含数据库连接检查
@router.get("/health")
async def health_check():
    try:
        # 尝试连接数据库
        db = SessionLocal()
        db.execute(text("SELECT 1"))
        db.close()
        db_status = "healthy"
    except Exception as e:
        logger.error(f"Database health check failed: {str(e)}")
        db_status = "unhealthy"
    
    return {
        "status": "healthy" if db_status == "healthy" else "degraded",
        "app": app_config.name,
        "version": app_config.version,
        "database": db_status
    }